package txtpack.Work;

import java.io.*;

public class Exam02 {
    public static void main(String[] args) {
        //文件拆分(切割)
        File target = new File("E:\\IO流练习\\OutPutStream\\skip\\skip.mp4");
        File dest = new File("E:\\IO流练习\\OutPutStream\\new2");
        skipSize(target, 1024 * 1024  * 10,dest);
    }

    private static void skipMp4(File file,int size, File dest) {
        if (!dest.getParentFile().exists()) { // 判断父级目录是否存在不存在创建父级目录
            dest.getParentFile().mkdirs();
        }
        try (FileInputStream in = new FileInputStream(file);) {
            byte[] bytes = new byte[(int) size];
            int readSize ;
            int i = 1;
            while ((readSize = in.read(bytes)) != -1) {
                File outputfile = new File(dest, i + ".mp4");
                try (FileOutputStream out = new FileOutputStream(outputfile)) {
                    out.write(bytes, 0, readSize);
                }
                i ++;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void skipSize(File file,long size, File dest){
        if (!dest.getParentFile().exists()) { // 判断父级目录是否存在不存在创建父级目录
            dest.getParentFile().mkdirs();
        }
        try (FileInputStream in = new FileInputStream(file);) {
            int i = 1;
            while (in.available() > 0){
                if (size > Integer.MAX_VALUE - 8){
                    long sum  = size;
                    File outputfile = new File(dest, i + ".mp4");
                    try(FileOutputStream out = new FileOutputStream(outputfile,true)) {
                        while (sum > Integer.MAX_VALUE - 8 && in.available() > 0) {
                            byte[] bytes = new byte[Integer.MAX_VALUE - 8];
                            int readSize = in.read(bytes);
                            out.write(bytes, 0, readSize);
                            sum -= readSize;
                        }
                        i ++;
                        if (in.available() > 0){
                            byte[] bytes = new byte[(int) sum];
                            int readSize = in.read(bytes);
                            out.write(bytes,0,readSize);
                        }
                        i ++ ;
                    }
                }else {
                    byte[] bytes = new byte[(int) size];
                    File outputfile1 = new File(dest, i + ".mp4");
                    try (FileOutputStream outfile = new FileOutputStream(outputfile1,true)){
                        int readSize = in.read(bytes);
                        outfile.write(bytes,0,readSize);
                    }
                    i ++;
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private static void split2(File targetFile, File destFile, long size) {
        if (!targetFile.exists()) {
            throw new RuntimeException("要切分的文件不存在！");
        }
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 加载要切分的数据源
        try (FileInputStream in = new FileInputStream(targetFile);) {
            int count = 0;
            while (in.available() > 0) { // 结束条件 流里面没有东西了
                // byte[] bytes = new byte[(int)size];
                if (size > Integer.MAX_VALUE - 8) { // 数组的最大安全长度是 Integer.MAX_VALUE - 8
                    long sum = size;
                    String name = ++count + ".tmp";
                    try(FileOutputStream out = new FileOutputStream(new File(destFile, name), true);) {
                        while (sum > Integer.MAX_VALUE - 8 && in.available() > 0) {
                            byte[] bytes = new byte[Integer.MAX_VALUE - 8];
                            int readSize = in.read(bytes);
                            out.write(bytes, 0, readSize);
                            sum -= readSize;
                        }
                        if (in.available() > 0) { // 一次的size位还没读完
                            byte[] bytes = new byte[(int) sum];
                            int readSize = in.read(bytes);
                            out.write(bytes, 0, readSize);
                        }  // 从输入流in里面读取 size长度的字节到输出流 out
                    }
                } else {
                    byte[] bytes = new byte[(int) size];
                    String name = ++count + ".tmp";
                    try (FileOutputStream out = new FileOutputStream(new File(destFile, name), true);) {
                        int readSize = in.read(bytes);
                        out.write(bytes, 0, readSize);
                    } // 从in 输入流读取size个字节 放到out 输出流中
                }
            }
            System.out.println("文件分割完毕，分成" + count + "个文件");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
